package org.ourgrid.acceptance.util.discoveryservice;

import org.easymock.classextension.EasyMock;
import org.ourgrid.acceptance.util.DiscoveryServiceAcceptanceUtil;
import org.ourgrid.common.interfaces.DiscoveryService;
import org.ourgrid.common.interfaces.DiscoveryServiceClient;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;

import br.edu.ufcg.lsd.commune.container.ContainerContext;
import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;
import br.edu.ufcg.lsd.commune.functionaltests.util.TestStub;
import br.edu.ufcg.lsd.commune.test.AcceptanceTestUtil;

public class Req_506_Util extends DiscoveryServiceAcceptanceUtil {

	public Req_506_Util(ContainerContext context) {
		super(context);
	}
	
	public void leaveCommunity(DiscoveryServiceComponent component, TestStub dsClientTestStub) {
		leaveCommunity(component, dsClientTestStub, true);
	}
	
	public void leaveCommunity(DiscoveryServiceComponent component, TestStub dsClientTestStub, boolean isCommunityMember) {

		CommuneLogger logger = component.getLogger();
		
		DiscoveryService discoveryService = (DiscoveryService) getDiscoveryServiceProxy();
		
		DiscoveryServiceClient dsClient = (DiscoveryServiceClient) dsClientTestStub.getObject();
		if (!isCommunityMember) {
			logger.warn("The client with ID [" + dsClientTestStub.getDeploymentID() + "] is not joined to the community.");
			EasyMock.replay(logger);
			AcceptanceTestUtil.publishTestObject(component, dsClientTestStub.getDeploymentID(), dsClient,
					DiscoveryServiceClient.class);
			discoveryService.leaveCommunity(dsClient);
				
			EasyMock.verify(logger);
		} else {
			discoveryService.leaveCommunity(dsClient);
		}
		
		EasyMock.reset(logger);
	}

}
